package com.platform.core.entity;

import com.alibaba.fastjson.JSONArray;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.platform.dto.InterfaceDto;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.GenericGenerator;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * Description: ncloud_sys_interface实体类
 *
 * @author Coder
 */
@Setter
@Getter
@Entity
@Table(name = "ncloud_sys_interface")
public class NcloudSysInterfaceEntity implements Serializable, Cloneable{

    /**
    * id
    */
    @Id
    @GenericGenerator(name = "system-uuid", strategy = "uuid")
    @GeneratedValue(generator = "system-uuid")
    private String id;

    /**
    * 接口名称
    */
    @Column(name = "interface_name" )
    private String interfaceName;

    /**
    * 接口地址
    */
    @Column(name = "interface_url" )
    private String interfaceUrl;

    /**
    * 接口描述
    */
    @Column(name = "interface_desc" )
    private String interfaceDesc;

    /**
    * 状态标志
    */
    @Column(name = "delflag" )
    private String delflag;

    /**
    * 接口状态
    */
    @Column(name = "status" )
    private String status;

    /**
    * 查看接口详细信息url
    */
    @Column(name = "detail_url" )
    private String detailUrl;

    /**
    * 创建时间
    */
    @Column(name = "create_date" )
    private LocalDateTime createDate;

    /**
    * 创建人
    */
    @Column(name = "create_by" )
    private String createBy;

    /**
    * 修改时间
    */
    @Column(name = "update_date" )
    private LocalDateTime updateDate;

    /**
    * 修改人
    */
    @Column(name = "update_by" )
    private String updateBy;

    /**
     * 接口类型
     */
    @Column(name = "type_id")
    private String typeId;

    /**
     * 请求类型
     */
    @Column(name = "req_type")
    private String reqType;

    /**
     * 请求参数
     */
    @Column(name = "req_param")
    private String reqParam;

    /**
     * 请求参数类型
     */
    @Column(name = "req_param_type")
    private String reqParamType;

    /**
     * 响应参数
     */
    @Column(name = "res_param")
    private String resParam;

    /**
     * 响应参数类型
     */
    @Column(name = "res_param_type")
    private String resParamType;

    /**
     * 请求示例
     */
    @Column(name = "req_example")
    private String reqExample;

    /**
     * 响应示例
     */
    @Column(name = "res_example")
    private String resExample;

    /**
     * 版本号
     */
    @Column(name = "version")
    private String version;

    /**
     * 是否被克隆 Y-是 N-否
     */
    @Column(name = "is_clone")
    private String isClone;

    /**
     * 是否被使用
     */
    @Column(name = "is_used")
    private String isUsed;

    /**
     * 克隆接口id
     */
    @Column(name = "parent_id")
    private String parentId;


    public NcloudSysInterfaceEntity cloneEntity() throws CloneNotSupportedException {
        return (NcloudSysInterfaceEntity) this.clone();
    }

    public static InterfaceDto of(NcloudSysInterfaceEntity interfaceEntity){
        InterfaceDto interfaceDto = new InterfaceDto();
        interfaceDto.setId(interfaceEntity.getId());
        interfaceDto.setInterfaceName(interfaceEntity.getInterfaceName());
        interfaceDto.setInterfaceDesc(interfaceEntity.getInterfaceDesc());
        interfaceDto.setInterfaceUrl(interfaceEntity.getInterfaceUrl());
        interfaceDto.setDelflag(interfaceEntity.getDelflag());
        interfaceDto.setCreateBy(interfaceEntity.getCreateBy());
        interfaceDto.setTypeId(interfaceEntity.getTypeId());
        interfaceDto.setCreateDate(interfaceEntity.getCreateDate());
        interfaceDto.setStatus(interfaceEntity.getStatus());
        interfaceDto.setUpdateBy(interfaceEntity.getUpdateBy());
        interfaceDto.setUpdateDate(interfaceEntity.getUpdateDate());
        interfaceDto.setDetailUrl(interfaceEntity.getDetailUrl());
        interfaceDto.setReqParam(getMaps(interfaceEntity.getReqParam()));
        interfaceDto.setResParam(getMaps(interfaceEntity.getResParam()));
        interfaceDto.setReqParamType(interfaceEntity.getReqParamType());
        interfaceDto.setReqExample(interfaceEntity.getReqParam());
        interfaceDto.setResExample(interfaceEntity.getResExample());
        interfaceDto.setReqParamType(interfaceEntity.getReqParamType());
        interfaceDto.setReqType(interfaceEntity.getReqType());
        interfaceDto.setVersion(interfaceEntity.getVersion());
        interfaceDto.setIsClone(interfaceEntity.getIsClone());
        interfaceDto.setIsUsed(interfaceEntity.getIsUsed());
        return interfaceDto;
    }

    private static List<Map<String, String>> getMaps(String param) {
        List<Map<String,String>> resParams = Lists.newArrayList();
        if (!StringUtils.isNotBlank(param)){
            return resParams;
        }
        JSONArray.parseArray(param, Map.class).forEach(map -> {
            Map<String, String> resParam = Maps.newHashMap();
            map.keySet().forEach(key -> resParam.put(key.toString(),String.valueOf(map.get(key))));
            resParams.add(resParam);
        });
        return resParams;
    }
}
